* Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
* Authors: Humza Khan and Pengfei Zhang
* Data: investment_telecomm

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

** converts strings to numeric
destring, replace

**reshape dataset
*unique identifier for each country
gen country_num = _n 

*Reshape data from wide to long, including controls 
reshape long investment, i(country_num) j(year)

*
tsset country_num year   

*Create treatment variable: timeToTreat 
gen timeToTreat = year - Eventtime 
gen treatment = cond(year >= Eventtime,1,0)

* preserve for analysis 
preserve

***TWFE OLS***
*drop observations based on time window
drop if timeToTreat < -17 
drop if timeToTreat > 17

*bc event windows. 
forvalues l = 0/17 {  
		gen L`l'event = timeToTreat == `l'
	} 
forvalues l = 1/16 { 
		gen F`l'event = timeToTreat ==-`l' 
} 
drop F1event  


*Dynamic Regression  
reghdfe investment  F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  


*** Graph: DiD Estimator Comparisons ***

*** Callaway and San'Anna *** SHOWS ALL ZERO VALUES
gen gvar = cond(Eventtime==., 0, Eventtime)  
csdid investment, ivar(country_num) time(year) gvar(gvar) notyet event
estat event, estore(cs)  


*** Sun and Abraham ***
gen lastcohort = Eventtime==r(max)
eventstudyinteract investment L*event F*event, vce(cluster country_num) absorb(country_num year) cohort(Eventtime) control_cohort(lastcohort)
matrix sa_b = e(b_iw)  
matrix sa_v = e(V_iw)

*** de Chaisemartin-D'Haultfoeuille ***
did_multiplegt_old investment country_num year treatment, robust_dynamic dynamic(12) placebo(4) breps(100) cluster(country_num) 
matrix dcdh_b = e(estimates)  
matrix dcdh_v = e(variances)

*HTE estimator graph

event_plot dcdh_b#dcdh_v cs sa_b#sa_v ols, ///
	stub_lag(Effect_# Tp# L#event L#event) stub_lead(Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(4) trimlag(7) noautolegend ///
	graph_opt( ///
		xtitle("{bf: Years Before and After Ratification}",size(small)) ytitle("{bf: Estimated Change in IT Investment}",size(small)) xlabel(-4(1)7, labsize(vsmall)) ylabel(-2(1)2, labsize(vsmall)) ///
		legend(order(2 "de Chaisemartin-D'Haultfoeuille" ///
				4 "Callaway-Sant'Anna" 6 "Sun-Abraham" 8 "OLS") rows(2) position(6) region(lcolor(black) lpattern(longdash)) size(small) placement(center) lalign(center)) ///
		xline(-1, lcolor(gs12) lpattern(dash)) yline(0, lpattern(dash) lcolor(gs12)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal) labsize(vsmall)) ///
	) ///
	lag_opt1(msymbol(O) color(orange)) lag_ci_opt1(color(orange)) ///
	lag_opt2(msymbol(Dh) color(blue)) lag_ci_opt2(color(blue)) ///
	lag_opt3(msymbol(Th) color(emerald)) lag_ci_opt3(color(emerald)) ///
	lag_opt4(msymbol(Sh) color(black)) lag_ci_opt4(color(black)) 
 
 
